package dapps.web.monitor;

import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;

import com.opensymphony.xwork2.ActionSupport;

import dapps.entity.monitor.Smvalue;
import dapps.entity.user.District;
import dapps.entity.user.Type;
import dapps.service.monitor.ElementManager;
import dapps.service.monitor.MonitorManager;
import dapps.service.user.UserManager;

@SuppressWarnings("serial")
public class TyperankAction extends ActionSupport {

	@Autowired
	private MonitorManager mManager;	
	@Autowired
	private ElementManager eManager;
	@Autowired
	private UserManager uManager;

	//private HttpServletRequest request = Struts2Utils.getRequest();
	

	private Page<Smvalue>  rankpage= new Page<Smvalue>(10);
	private List<Smvalue>  ranklist;



	private String reportDate;
	private Long typeid;  
	private Long districtid;  
	private List<Type> typelist;
	private int orderid;
	private List<District> districtlist;

	@Override
	public String execute() throws Exception {
		int i=0;
		if(SpringSecurityUtils.getAuthority("A_ALL_DISTRICT")){
			districtlist = uManager.getDistrictlist(null,null, null);
			i=1;
		}
		//未选 或 未跨区授权
		if(districtid==null||i==0) {
			Long udistrictid = SpringSecurityUtils.getCurrentUser().getDistrict().getId();
			if(udistrictid!=null&&udistrictid>0L){
				districtid = udistrictid;
			}else{
				districtid=null;
			}
		}
		typelist = eManager.getTypelist(new Criterion[]{Restrictions.gt("tid", new Integer(0)),Restrictions.eq("chooseable", 1)},null, new Order[] {Order.asc("tid")});
		if (typeid != null && typeid >= 0&&districtid!=null) {
			if (orderid == 1)
				rankpage.setOrderBy("s.mincomelmsum");
			else if (orderid == 2)
				rankpage.setOrderBy("s.profitaftaxlmsum");
			else if (orderid == 3)
				rankpage.setOrderBy("s.costlmsum");
			rankpage.setOrder("desc");
			//rankpage = mManager.getRank(rankpage, reportDate, typeid,districtid);
			ranklist = mManager.getRankList(rankpage, reportDate, typeid,districtid);
		}
		return SUCCESS;
	}


	public String getReportDate() {
		return reportDate;
	}

	public void setReportDate(String reportDate) {
		this.reportDate = reportDate;
	}



	public Long getDistrictid() {
		return districtid;
	}


	public void setDistrictid(Long districtid) {
		this.districtid = districtid;
	}


	public Long getTypeid() {
		return typeid;
	}

	public void setTypeid(Long typeid) {
		this.typeid = typeid;
	}

	public List<Type> getTypelist() {
		return typelist;
	}


	public List<District> getDistrictlist() {
		return districtlist;
	}


	public Page<Smvalue> getRankpage() {
		return rankpage;
	}


	public void setRankpage(Page<Smvalue> rankpage) {
		this.rankpage = rankpage;
	}


	public int getOrderid() {
		return orderid;
	}


	public void setOrderid(int orderid) {
		this.orderid = orderid;
	}
	public List<Smvalue> getRanklist() {
		return ranklist;
	}

}
